package ttsolver.variables;

import ttsolver.input.Track;
import ttsolver.input.Train;

public class Bcontrol extends DefaultVariable {

	private Train train1;
	private Train train2;
	private Track track;
	private int milepost;

	public Bcontrol() {
		super(VariableType.BCONTROL);
	}

	public Bcontrol(Train t1, Train t2, int milepost) {
		super(VariableType.BCONTROL);
		this.train1 = t1;
		this.train2 = t2;
		this.milepost = milepost;
	}

	public Bcontrol(Train t1, Track track) {
		super(VariableType.BMOW);
		this.train1 = t1;
		this.track = track;
	}

	public Train getTrain1() {
		return train1;
	}

	public void setTrain1(Train train1) {
		this.train1 = train1;
	}

	public Train getTrain2() {
		return train2;
	}

	public void setTrain2(Train train2) {
		this.train2 = train2;
	}

	public Track getTrack() {
		return track;
	}

	public void setTrack(Track track) {
		this.track = track;
	}

	public int getMilepost() {
		return milepost;
	}

	public void setMilepost(int milepost) {
		this.milepost = milepost;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((train1 == null) ? 0 : train1.getId().intValue());
		result = prime * result
				+ ((train2 == null) ? 0 : train2.getId().intValue());
		result = prime * result
				+ ((track == null) ? 0 : track.getNodeOne().getId().intValue());
		result = prime * result
				+ ((track == null) ? 0 : track.getNodeTwo().getId().intValue());
		result = prime * result + milepost;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;

		Bcontrol other = (Bcontrol) obj;

		if (train1 == null) {
			if (other.train1 != null)
				return false;
		} else if (!train1.equals(other.train1)) {
			return false;
		}

		if (train2 == null) {
			if (other.train2 != null)
				return false;
		} else if (!train2.equals(other.train2)) {
			return false;
		}

		if (track == null) {
			if (other.track != null)
				return false;
		} else if (!track.equals(other.track)) {
			return false;
		}

		if (milepost != other.milepost) {
			return false;
		}

		return true;
	}

	@Override
	public int compareTo(Object obj) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;

		Bcontrol other = (Bcontrol) obj;

		if (this == other)
			return EQUAL;

		if (this.train1 != null && other.train1 != null) {
			if (this.train1.getId() < other.train1.getId())
				return BEFORE;
			if (this.train1.getId() > other.train1.getId())
				return AFTER;
		}

		if (this.train2 != null && other.train2 != null) {
			if (this.train2.getId() < other.train2.getId())
				return BEFORE;
			if (this.train2.getId() > other.train2.getId())
				return AFTER;
		}

		if (this.getTrack().getNodeOne() != null
				&& other.getTrack().getNodeOne() != null) {
			if (this.getTrack().getNodeOne().getId() < other.getTrack()
					.getNodeOne().getId())
				return BEFORE;
			if (this.getTrack().getNodeOne().getId() > other.getTrack()
					.getNodeOne().getId())
				return AFTER;
		}

		if (this.getTrack().getNodeTwo() != null
				&& other.getTrack().getNodeTwo() != null) {
			if (this.getTrack().getNodeTwo().getId() < other.getTrack()
					.getNodeTwo().getId())
				return BEFORE;
			if (this.getTrack().getNodeTwo().getId() > other.getTrack()
					.getNodeTwo().getId())
				return AFTER;
		}

		if (this.milepost < other.milepost)
			return BEFORE;
		if (this.milepost > other.milepost)
			return AFTER;

		return EQUAL;
	}

	@Override
	public String toString() {
		if (!printVar)
			return "";

		String s = (train1 != null) ? String.valueOf(train1.getId()) : "";
		s += (train2 != null) ? "," + String.valueOf(train2.getId()) : "";
		s += (track != null) ? "," + String.valueOf(track.getNodeOne().getId())
				: "";
		s += (track != null) ? "," + String.valueOf(track.getNodeTwo().getId())
				: "";
		s += "," + String.valueOf(milepost);
		if (type == VariableType.BCONTROL)
			return "B_(" + s + ")";
		else
			return "BB_(" + s + ")";

	}
}
